package com.example.pwj.ffstudy.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.pwj.ffstudy.R;
import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;


/**
 * Created by admin on 2017/3/30.
 */

public abstract class BaseFragment extends RxFragment {
    protected Context mContext;
    protected View rootView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        ButterKnife.bind(this,rootView);
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mContext = null;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    protected abstract int getLayoutId();

    protected abstract void init() ;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    protected void onInvisible(){

    }

    protected void onVisible(){

    }

    protected void back() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        if (fm.popBackStackImmediate()) {
        } else {
            getActivity().finish();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().overridePendingTransition(R.anim.activity_right_in,R.anim.activity_left_out);
    }
}
